﻿package _3_Panel;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
 * 2011-6-4 21:29:37
 * @author Administrator
 */
public class TJTabbedPane {
    JFrame frame;
    Container contentPane;
    JTabbedPane tabbedPane;
    JSplitPane splitPane;
    JButton southButton,northButton,eastButton,westButton,addButton,removeButton;
    JPanel buttonPane;
    public TJTabbedPane(){
        frame=new JFrame("选项卡测试");
        contentPane=frame.getContentPane();
        splitPane=new JSplitPane();
        splitPane.setResizeWeight(0);
        splitPane.setEnabled(false);
        contentPane.add(splitPane);
        setButton();
        splitPane.setLeftComponent(buttonPane);
        setTabbedPane();
        splitPane.setRightComponent(tabbedPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public void setButton(){
        southButton=new JButton("南");
        southButton.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
        southButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                tabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
            }
        });
        northButton=new JButton("北");
        northButton.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
        northButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                tabbedPane.setTabPlacement(JTabbedPane.TOP);
            }
        });
        eastButton=new JButton("东");
        eastButton.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
        eastButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                tabbedPane.setTabPlacement(JTabbedPane.RIGHT);
            }
        });
        westButton=new JButton("西");
        westButton.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
        westButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                tabbedPane.setTabPlacement(JTabbedPane.LEFT);
            }
        });
        addButton=new JButton("增加");
        addButton.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
        addButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                tabbedPane.addTab("第"+(tabbedPane.getTabCount()+0)+"个选项卡", new JPanel(){
                    /**
                     * 
                     */
                    private static final long serialVersionUID = 1L;

                    public void paint(Graphics g){
                        int i=tabbedPane.indexOfComponent(this);
                        g.setColor(Color.white);
                        g.fillRect(0, 0, this.getWidth(), this.getHeight());
                        Font font=new Font("楷体",Font.PLAIN,40);
                        g.setFont(font);
                        g.setColor(Color.red);
                        String str="这是第"+i+"个选项卡";
                        Rectangle2D rect=font.getStringBounds(str, new FontRenderContext(new AffineTransform(), true, true));
                        g.drawString(str, (int)((this.getWidth()-rect.getWidth())/2), (int)(rect.getHeight()));
                    }
                });
                tabbedPane.setBackgroundAt(tabbedPane.getTabCount()-1, Color.white);
                tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
                tabbedPane.repaint();
//                System.out.println(tabbedPane.getTabRunCount());
                System.out.println(tabbedPane.getUI());
            }
        });
        removeButton=new JButton("删除");
        removeButton.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
        removeButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                tabbedPane.removeTabAt(tabbedPane.getTabCount()-1);
            }
        });
        Box box=Box.createVerticalBox();
        box.add(Box.createVerticalGlue());
        box.add(northButton);
        box.add(Box.createVerticalStrut(10));
        box.add(southButton);
        box.add(Box.createVerticalStrut(10));
        box.add(eastButton);
        box.add(Box.createVerticalStrut(10));
        box.add(westButton);
        box.add(Box.createVerticalStrut(10));
        box.add(addButton);
        box.add(Box.createVerticalStrut(10));
        box.add(removeButton);
        box.add(Box.createVerticalGlue());
        buttonPane=new JPanel();
        buttonPane.add(box);
    }
    public void setTabbedPane(){
        tabbedPane=new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.setBackground(Color.WHITE);
        tabbedPane.setForeground(Color.red);
        tabbedPane.setPreferredSize(new Dimension(400,400));
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        tabbedPane.addChangeListener(new ChangeListener(){
            public void stateChanged(ChangeEvent e) {
                Object src=e.getSource();
                System.out.println(src);
            }
        });
    }
    public static void main(String args[]){
        new TJTabbedPane();
    }
}
